package com.leo.stockmarket.scheduler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.leo.stockmarket.bean.Article;
import com.leo.stockmarket.bean.EmailBean;
import com.leo.stockmarket.bean.TokenBean;
import com.leo.stockmarket.service.ArticleService;
import com.leo.stockmarket.service.EmailService;
import com.leo.stockmarket.service.IMailService;
import com.leo.stockmarket.service.TokenService;
import com.leo.stockmarket.util.NetUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 890231 lxj
 * @version 1.0
 * @date 2020/8/18 16:00
 */
@Component
@Lazy(false)
public class GenArticleScheduler {

    private String LIST_URL = "https://admin.gbhome.com/api/v4/common/3in1/subscribe";
    private String DETAIL_URL = "https://admin.gbhome.com/api/common/zlArticle/detail/";
    private String PARAM_MAX = "pageNum=1&pageSize=600";
    private String PARAM_10 = "pageNum=1&pageSize=15";
    //private String TOKEN = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJ3eFVzZXJJZCI6MTAwNDM3Miwic3ViIjoiMTAwNDM3MiIsIm1vYmlsZVBob25lIjoiMTg4MTMyOTIwMzEiLCJjaGFubmVsIjoiUEMiLCJleHAiOjE2MDAzNTQ2MTMsImlhdCI6MTU5Nzc2MjYxM30.BTKIXbAVkhf0TjjRdbTiPonU9vSKSc-7lFA6_beuJTjtBpn8f62wr0sGb6UQiOGnZxaHc9IULcA4-CPoX-wifA";

    @Autowired
    private ArticleService articleService;

    @Autowired
    private IMailService mailService;

    @Autowired
    private EmailService emailService;

    @Autowired
    private TokenService tokenService;

    //@Scheduled(cron = "0 0/10 * * * ?")
    public void genAllArticles() {
        TokenBean token = tokenService.queryRecentTokenStr();
        List<Article> articles = new ArrayList<>();
        String articleListStr = NetUtils.sendGet(LIST_URL, PARAM_MAX, "");
        JSONObject jsonObject = (JSONObject) JSONObject.parseObject(articleListStr).get("data");
        List<JSONObject> objectList = (List<JSONObject>) jsonObject.get("records");
        for (JSONObject o: objectList) {
            Article article = JSON.toJavaObject(o, Article.class);
            article.setBusinessId(UUID.randomUUID().toString());
            if (article.getType().equals("zlArticle")) {
                String articleDetailStr = NetUtils.sendGet(DETAIL_URL + "/" + article.getSubjectId(), "", token.getTokenStr());
                JSONObject data = (JSONObject) JSONObject.parseObject(articleDetailStr).get("data");
                JSONObject zlArticle = (JSONObject) data.get("zlArticle");
                article.setDetail((String) zlArticle.get("detail"));
            }
            articleService.insert(article);
            articles.add(article);
        }
    }

    //工作日开市时间
    @Scheduled(cron = "0/15 * 8-16 ? * MON-FRI")
    public void genCurrentArticlesWorkDate() {
        genCurrentArticles();
    }

    //工作日收市时间
    @Scheduled(cron = "0 0/30 17-23 ? * MON-FRI")
    public void genCurrentArticlesWorkDateClose() {
        genCurrentArticles();
    }

    @Scheduled(cron = "0 0 16-23 ? * 1,7 ")
    @Transactional(propagation = Propagation.REQUIRED)
    public void genCurrentArticlesWeekend() {
        genCurrentArticles();
    }

    public void genCurrentArticles(){
        TokenBean token = tokenService.queryRecentTokenStr();
        String articleListStr = NetUtils.sendGet(LIST_URL, PARAM_10, "");
        JSONObject jsonObject = (JSONObject) JSONObject.parseObject(articleListStr).get("data");
        List<JSONObject> objectList = (List<JSONObject>) jsonObject.get("records");
        for (JSONObject o : objectList) {
            Article article = JSON.toJavaObject(o, Article.class);
            List<Article> articleList = articleService.queryListBySubjectId(article.getSubjectId(), article.getType());
            if (articleList.size() < 1) {
                article.setBusinessId(UUID.randomUUID().toString());
                if (article.getType().equals("zlArticle")) {
                    String articleDetailStr = NetUtils.sendGet(DETAIL_URL + "/" + article.getSubjectId(), "", token.getTokenStr());
                    JSONObject data = (JSONObject) JSONObject.parseObject(articleDetailStr).get("data");
                    JSONObject zlArticle = (JSONObject) data.get("zlArticle");
                    article.setDetail((String) zlArticle.get("detail"));
                }
                articleService.insert(article);
                String detail = "";
                String type = article.getType();
                String title = article.getTitle();
                if (type.equals("zlArticle")) {
                    detail = article.getDetail();
                } else {
                    detail = article.getContent();
                }

                List<EmailBean> emailBeanList = emailService.queryEmailList();
                for (EmailBean bean : emailBeanList) {
                    mailService.sendHtmlMail(bean.getEmailAddress(), title, detail);
                }
                /*mailService.sendHtmlMail("3114002944@mail2.gdut.edu.cn",title,detail);
                mailService.sendHtmlMail("1600534677@qq.com",title,detail);  //鑫爷
                mailService.sendHtmlMail("15060340235@xs.hnit.edu.cn",title,detail);   //发爷
                mailService.sendHtmlMail("592921046@qq.com",title,detail);   //须臾
                mailService.sendHtmlMail("516628970@qq.com",title,detail);   //书记*/
            }
        }
    }

}
